package com.sisi.web;

import entity.Student811;
import util.JavaUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet("/student811")
public class Student811Servlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");//从浏览器得到的添加值是utf-8类型的

        String st = req.getParameter("st");
        if (st == null || st.equals("")){
            st = "selectAll";
        }
        switch (st) {
            case "selectAll" :
                selectAll(req,resp);
                break;
            case "deleteById" :
                deleteById(req,resp);
                break;
            case "insert" :
                insert(req,resp);
                break;
            case "selectById" :
                selectById(req,resp);
                break;
            case "update" :
                update(req,resp);
                break;
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        int id = Integer.parseInt(req.getParameter("id"));
        String name = req.getParameter("name");
        int age = Integer.parseInt(req.getParameter("age"));
        String gender = req.getParameter("gender");
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JavaUtil.getConnection();
            String sql = "UPDATE student811 SET  name=?,age=?,gender=? WHERE id=?";
            statement = connection.prepareStatement(sql);
            statement.setString(1,name);
            statement.setInt(2,age);
            statement.setString(3,gender);
            statement.setInt(4,id);
            int count = statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JavaUtil.close(connection,statement,null);
        }
        resp.sendRedirect(req.getContextPath() + "/student811?st=selectAll");
    }

    private void selectById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int id = Integer.parseInt(req.getParameter("id"));
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        Student811 student811 = null;
        try {
            connection = JavaUtil.getConnection();
            String sql = "SELECT id,name,age,gender FROM student811 WHERE id=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,id);
            resultSet = statement.executeQuery();
            while (resultSet.next()){
                int id1 = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");
                student811 = new Student811(id1,name,age,gender);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JavaUtil.close(connection,statement,resultSet);
        }
        req.setAttribute("st8111",student811);
        req.getRequestDispatcher("/update_student811.jsp").forward(req,resp);

    }

    private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        ArrayList<Student811> arrayList = new ArrayList<>();


        try {
            connection = JavaUtil.getConnection();
            String sql = "SELECT id,name,age,gender FROM student811";
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            while (resultSet.next()){
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");
                Student811 student811 = new Student811(id,name,age,gender);
                arrayList.add(student811);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JavaUtil.close(connection,statement,resultSet);
        }
        req.setAttribute("st811",arrayList);
        req.getRequestDispatcher("/student811_list.jsp").forward(req,resp);





    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        String id = req.getParameter("id");
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JavaUtil.getConnection();
            String sql = "DELETE FROM student811 WHERE id = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,Integer.parseInt(id));
            int count = statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JavaUtil.close(connection,statement,null);
        }
        resp.sendRedirect(req.getContextPath() + "/student811?st=selectAll");


    }

    private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        String name = req.getParameter("name");
        String age = req.getParameter("age");
        String gender = req.getParameter("gender");
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JavaUtil.getConnection();
            String sql = "INSERT INTO student811(name,age,gender) VALUES(?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1,name);
            statement.setInt(2,Integer.parseInt(age));
            statement.setString(3,gender);
            int count = statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JavaUtil.close(connection,statement,null);
        }

        resp.sendRedirect(req.getContextPath() + "/student811?st=selectAll");
    }
}
